package com.zx.mes.hyl.common.rest;

import com.zx.mes.hyl.common.biz.BaseBiz;
import com.zx.mes.hyl.common.msg.ObjectRestResponse;
import com.zx.mes.hyl.common.msg.TableResultResponse;
import com.zx.mes.hyl.common.util.Query;
import com.zx.mes.hyl.common.util.ReflectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 基础controller类
 *
 * @author hyl
 * @date 2018-1-27
 */
public class BaseController<Biz extends BaseBiz,Entity> {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Biz baseBiz;

    private static final String REFLECTION_FIELD_ID="id";
    private static final String REFLECTION_FIELD_CREATEDATETIME = "createdatetime";
    private static final String REFLECTION_FIELD_MODIFYDATETIME = "modifydatetime";
    /**
     * 使用要求:所有的表都必需有唯一key值,且名称为 REFLECTION_FIELD_NAME (id)
     * @param entity 实体类(pojo)
     * @return ObjectRestResponse
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> add(Entity entity){
        //系统自动生成数据库主键ID
        if (ReflectionUtils.hasField(entity,REFLECTION_FIELD_ID)) {
            ReflectionUtils.setFieldValue(entity,REFLECTION_FIELD_ID, UUID.randomUUID().toString());
        }
        //添加时系统自动记录创建时间
        if (ReflectionUtils.hasField(entity,REFLECTION_FIELD_CREATEDATETIME)) {
            ReflectionUtils.setFieldValue(entity,REFLECTION_FIELD_CREATEDATETIME, new Date());
        }
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Entity> get(@PathVariable String id){
        return new ObjectRestResponse<Entity>().rel(true).data(baseBiz.selectById(id));
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Entity> update( Entity entity){
        //更新时间
        if (ReflectionUtils.hasField(entity,REFLECTION_FIELD_MODIFYDATETIME)) {
            ReflectionUtils.setFieldValue(entity,REFLECTION_FIELD_MODIFYDATETIME, new Date());
        }
        if (ReflectionUtils.hasField(entity,REFLECTION_FIELD_CREATEDATETIME)) {
            ReflectionUtils.setFieldValue(entity,REFLECTION_FIELD_CREATEDATETIME, null);
        }
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Entity> remove(@PathVariable String id){
        baseBiz.deleteById(id);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public List<Entity> all(){
        return baseBiz.selectListAll();
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Entity> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return baseBiz.selectByQuery(query);
    }

    public String getCurrentUserName(){
        String authorization = request.getHeader("Authorization");
        return new String(Base64Utils.decodeFromString(authorization));
    }
}
